#include "maindeviceform.h"
#include "ui_maindeviceform.h"
#include <QGlobalStatic>
#include <QVBoxLayout>
#include "parsecandata.h"
#include "mainscene.h"

Q_GLOBAL_STATIC(MainDeviceForm,m_instance)

MainDeviceForm::MainDeviceForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainDeviceForm)
{    
    ui->setupUi(this);
    connect(ParseCANDataIns,&ParseCANData::flushUIBMS204,this,&MainDeviceForm::on_flushUIBMS204,Qt::QueuedConnection);

    initGraphicsView();
    ui->xslider_fs->setEnabled(false);
}

MainDeviceForm::~MainDeviceForm()
{
    delete ui;
}

MainDeviceForm *MainDeviceForm::Instance()
{
    return m_instance;
}

void MainDeviceForm::initGraphicsView()
{
    ui->graphicsView->setScene(MainSceneIns);
    ui->graphicsView->setStyleSheet("background-color:#0D0D0D;border:none;");
    ui->graphicsView->setRenderHint(QPainter::Antialiasing);
    ui->graphicsView->setTransform(QTransform()); // 重置缩放/平移
    ui->graphicsView->centerOn(0, 0);             // 居中显示
}

void MainDeviceForm::on_flushUIBMS204(double battVolt, double battCurr, double soc)
{
    //刷新204相关控件的状态和信息
    ui->BattVolt->setValue(battVolt);
    ui->BattCurr->setValue(battCurr);

    ui->SOC->setValue(soc);
}

void MainDeviceForm::on_xslider_fs_valueChanged(int value)
{
    QByteArray canData = MainSceneIns->GetCurrentByte();
    // 1. 数值范围校验 (0~255)
    //const quint8 power = static_cast<quint8>(qBound(0, value, 255));
    //风扇功率控制
//    QByteArray canData(8,0x00);
    bool state = false;

    if(value != 0) {
        canData[1] = 0x01;
        canData[2] = static_cast<char>(value);
        state = true;
    }
    else {
        canData[1] = 0x00;
        canData[2] = static_cast<char>(value);
        state = false;
    }

    MainSceneIns->SetFSState(state);
    MainSceneIns->SetByte(canData);
    emit sendData(0x37B,canData);
}


void MainDeviceForm::on_switchButton_relay_checkedChanged(bool checked)
{
    MainSceneIns->SetEnableRelay(checked);
    ui->xslider_fs->setEnabled(checked);
}

